<template>
  <div class="app-container" :style="height">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" class="work-search-form">
      <el-form-item label="名称" prop="templateName">
        <el-input v-model="form.templateName" placeholder="请输入名称" />
      </el-form-item>
      <el-form-item label="内容" prop="templateContent">
        <Editor v-model="form.templateContent" style="height: 240px;" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align:center;">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
  import {addTemplate, updateTemplate, getTemplateByName } from "@/api/system/template";
  import Editor from '@/components/Editor';

  export default {
    name: "addEditTemplate",
    components: {
      Editor
    },
    data() {
      return {
        height:"max-height:"+((document.documentElement.clientHeight||document.body.clientHeight)-110)+"px;overflow-y:auto",

        form: {},
        // 表单校验
        rules: {
          templateName: [
            { required: true, message: "名称不能为空", trigger: "blur" }
          ],
          templateContent: [
            { required: true, message: "内容不能为空", trigger: "blur" }
          ],
        },
      }
    },
    created() {
      this.reset();
      const row = this.$route.params && this.$route.params.row;
      if (row!=null && row!=undefined && row!=''){
        this.form = row;
      }
    },
    methods: {
      // 表单重置
      reset() {
        this.form = {
          templateId: null,
          templateName: null,
          templateContent: null,
          status: "0",
          createName: null,
          updateName: null,
          updateTime: null,
          remake: null
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancel() {
        this.$router.go(-1);// 返回
        //关闭子页面
        this.$store.state.tagsView.visitedViews.splice(this.$store.state.tagsView.visitedViews.findIndex(item => item.path === this.$route.path), 1);
        var view = {
          name: 'Template',
        }
        this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
          this.$router.push(this.$store.state.tagsView.visitedViews[this.$store.state.tagsView.visitedViews.length - 1].path);
        })
      },
      /** 提交按钮 */
      submitForm:function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            getTemplateByName(this.form.templateName).then(response => {
              if (response.data!=undefined && response.data.templateId!=null && response.data.templateId!=this.form.templateId){
                this.msgError("模板名称已存在，提交失败，请修改！");
              }else {
                if (this.form.templateId != null) {
                  updateTemplate(this.form).then(response => {
                    if (response.code === 200) {
                      this.msgSuccess("修改成功");
                      this.cancel();
                    }
                  });
                } else {
                  addTemplate(this.form).then(response => {
                    if (response.code === 200) {
                      this.msgSuccess("新增成功");
                      this.cancel();
                    }
                  });
                }
              }
            })
          }
        });
      },
    }
  }
</script>
